//
//  ChatP2PDelegate.m
//  TcpServer
//
//  Created by scarlettsun on 12-7-31.
//  Copyright (c) 2012年 tencent. All rights reserved.
//

#import "PairChatPacketCenter.h"
#import "SockPacket.h"
#import "MiniSocket.h"
#import "MainSocketDelegate.h"

@implementation PairChatPacketCenter

-(void)MessageDispatch: (SockPacket *)packet
{
    UTF8Char * bytes = (UTF8Char *)[packet.packetContent bytes] + 8;
    NSUInteger subFun =*((NSUInteger *) bytes);
    switch (subFun)
    {
        case 0:
        {
            NSString *msg = [[NSString alloc] initWithBytes:(bytes+8)
                                                     length:(packet.packetLen-16) encoding:NSUTF8StringEncoding];
            if(msg)
            {
                //包格式为：收方qq/发方qq/消息
                NSArray *msgArray = [msg componentsSeparatedByString:@"/"];
                NSUInteger QQ = (NSUInteger)[[msgArray objectAtIndex:0] intValue];
                NSString *chatMsg = [msgArray objectAtIndex:2];
                
                for (MiniSocket *ms in [MainSocketDelegate connectedSocketsQueue]) {
                    if (QQ == ms.userId) {
                        NSLog(@"log delegate message--->收到---%@ subfun is %lu\r\n",chatMsg,subFun);
                        NSUInteger length = packet.packetLen;
                        NSMutableData *data = [NSMutableData dataWithBytes:&(length) length:sizeof(NSUInteger)];
                        [data appendBytes:[packet.packetContent bytes] length:length];
                        
                        [ms.miniAsynSocket writeData:data withTimeout:-1 tag:0];
                        break;
                    }
                }
            }
            else
            {
                NSLog(@"Error converting received data into UTF-8 String");
            }
            break;
        }
        case 1:
            
            break;
        default:
            break;
}

}
@end
